package com.ym.controller.admin;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.redis.RedisUtil;
import com.ym.medical.domain.entity.SysDictEntity;
import com.ym.medical.domain.param.SysDictQueryParam;
import com.ym.medical.domain.param.SysDictUpdateParam;
import com.ym.medical.domain.vo.SysDictListPcVO;
import com.ym.medical.service.ISysDictSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;

import static com.ym.common.constant.ValidRule.Add;
import static com.ym.common.constant.ValidRule.Update;

/**
 * 字典Controller
 *
 * @author ym
 * @date 2020-08-17
 */
@Api(tags = "admin-字典")
@RestController
@Validated
@RequestMapping("/sys/dict")
@Slf4j
public class SysDictController extends BaseController {

	@Autowired
	private ISysDictSV sysDictSV;

	@Autowired
	private RedisUtil redisUtil;

	@ApiOperation("查询字典列表")
	@Log(dataBase = false, printBody = false, printQuery = true)
	@GetMapping("/list")
	public APIResponse<PageInfo<SysDictListPcVO>> list(@Valid SysDictQueryParam queryParam) {
		PageDomain.startPage();
		List<SysDictListPcVO> list = sysDictSV.selectListPC(queryParam);
		return successPage(list);
	}

	@ApiOperation("获取字典详细信息")
	@Log(dataBase = false, printBody = false, printQuery = true)
	@GetMapping(value = "/info")
	public APIResponse<SysDictEntity> info(@RequestParam Long id) {
		SysDictEntity data = sysDictSV.selectById(id);
		return successData(data);
	}

	@ApiOperation("新增字典")
	@Log(printQuery = false)
	@PostMapping("/insert")
	public APIResponse insert(@RequestBody @Validated(value = Add.class) SysDictUpdateParam updateParam) {
		Long id = sysDictSV.insertPC(updateParam);
		return successData(id);
	}

	@ApiOperation("修改字典")
	@Log(printQuery = false)
	@PostMapping("/update")
	public APIResponse update(@RequestBody @Validated(value = Update.class) SysDictUpdateParam updateParam) {
		int result = sysDictSV.updateByIdPC(updateParam);
		return successData(result);
	}

	@ApiOperation("删除字典")
	@Log(printQuery = false)
	@PostMapping("/delete")
	public APIResponse delete(@RequestBody @Valid @NotNull Long[] ids) {
		int result = sysDictSV.deleteByIds(ids);
		return successData(result);
	}

	/**
	 * 缓存刷新
	 *
	 * @return
	 */
	@PostMapping("/cache/refresh")
	public APIResponse<String> cacheRefresh() {
		Collection<String> keys = redisUtil.keys(Constants.System.CACHE_KEY_PREFIX + ":" + Constants.System.CACHE_DICT + "*");
		keys.forEach(key -> {
			log.info("删除字典Key:{}", key);
			redisUtil.del(key);
		});
		return successMsg("缓存刷新成功!");
	}
}
